//
// FILE	
// 				fum_kernell.h
// AUTHOR
//				Filipe Teixeira Campos
// PROPERTY
//				This code is property of Filipe Teixeira Campos.
//				This code has no warranty, use at Your own risk.
//				The intelectual property is of of Filipe Teixeira Campos.
//
// DESC		 
//				this is the includes of the kernell
//

#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/sleep.h>

//
// defines
// 
//#define FU_PROT_MODE										// if defined make protect mode

#define FUM_MAX_TASKS				3						// max number of tasks
#define FUM_NUM_REGS				35						// 32 regs + SR + 2 PC
#define FUM_TASK_STACKSIZE			(25+FUM_NUM_REGS)		// bytes for task stack
#define FUM_KRNL_STACKSIZE			(10+FUM_NUM_REGS)		// bytes for kernel stack
#define FUM_STARTOFSTACK			0x250					// address of stack for first task			
#define FUM_T0_1MS					248						// valor de T0 para 1ms
															// 0.001 / (1/(clock / 1024)) 
#define FUMFUNC						__attribute__ ((naked))
	#define FUMTSK_STOP					0						// task state = STOPPED and good to go
	#define FUMTSK_RUN					1						// task state = RUN the task
	#define FUMTSK_WAIT					2						// task state = WAIT for a lock,...
	#define FUMTSK_SLEEP				3						// task state = SLEEP, sleeping order
#define NUM_LOCKS					4						// max number of locks	(semaforos)
	#define FUMLCK_FREE					0						// lock is free
	#define FUMLCK_INUSE				1						// lock is in use

//
// structs
//


typedef struct 
{
	// specifies a lock (Critical section)
	
	unsigned char 	ucState ;							// state of the lock, 0=free, 1-ownwed
	unsigned char 	ucTidOwner;							// task that ownes the lock
	unsigned char 	ucLocksTid [FUM_MAX_TASKS];			// tasks that are locked waiting for this 
	unsigned char 	ucLocksIdx;
} RT_LOCK;				


typedef struct
{
	// describes a task. It has stack in it
	unsigned char 	ucTid;								// task id
	unsigned char 	ucPrio;								// priority
	unsigned char 	ucState;							// task state, 0=stop, 1=run, 2=wait, 3=sleep
	unsigned short 	ucSleep;							// sleep time , 1.. ms,for sleep state						
	unsigned char		ucaStack[FUM_TASK_STACKSIZE];		// array for put stack of this task	
	unsigned char 	ucSPL;								// stack pointer low
	unsigned char 	ucSPH;								// stack pointer high
} RT_FUMEGA_TASK;

typedef struct
{
	// memory for the kernel, it has:
	//	- tasks
	// 	- locks
	//	- dummy stack (sleep mode loop)
	
	unsigned char 	numTasks;							// number of tasks
	unsigned char 	curTask;							// current task
	unsigned char 	ucSPL;								// stack pointer low
	unsigned char 	ucSPH;								// stack pointer high
	RT_FUMEGA_TASK 		Task[FUM_MAX_TASKS];				// tasks mem
	unsigned char		ucaStack[FUM_KRNL_STACKSIZE];		// array for put stack of kernel
	RT_LOCK				aLocks[NUM_LOCKS];					// array of semafors
	
	unsigned char 	CpuFree;							// counter of free cpu
	unsigned char 	CpuRun;								// counter of occupied cpu
	
} RT_FUMEGA;

//
// vars
//
RT_FUMEGA fumega;											// kernel var

//
// prototypes
//
void FG_initKernell(void);
void FG_addTask(void (*task)(),unsigned char prio);
void FG_startKernell(void);
void FG_tasksleep(unsigned char tid,unsigned short ms);
void FG_Lock(unsigned char tid,unsigned char lock);
void FG_UnLock(unsigned char tid,unsigned char lock);

